package lc.com.architecture.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.components.support.RxFragment;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import lc.com.architecture.i.IPresenter;
import lc.com.architecture.i.IView;

/**
 * @author LiuChao
 * @describe
 * @date 2017/11/3
 * @contact email:450127106@qq.com
 */

public abstract class BaseFragment<P extends IPresenter> extends RxFragment implements IView {

    public BaseActivity mActivity;
    public LayoutInflater mLayoutInflater;
    public View mRootView;
    @Inject
    protected P mIPresenter;
    private Unbinder mUnbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mLayoutInflater = inflater;
        mRootView = getContentView();
        mUnbinder = ButterKnife.bind(this, mRootView);
        initView(mRootView);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = (BaseActivity) getActivity();
        componentInject();
        initData();
    }

    public abstract View getContentView();

    public abstract void initData();

    public abstract void initView(View rootView);

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != Unbinder.EMPTY) mUnbinder.unbind();
    }

    /**
     * 依赖注入的入口
     */
    protected abstract void componentInject();
}
